abstract class Animal{
    public abstract void makeSound();

    public void sleep(){
        System.out.println("Zzz...");
    }
}

class Dog extends Animal{
    @Override
    public void makeSound(){
        System.out.println("Woof!Woof!");
    }
}

interface Car{
    void carColor();
    default void shape(){
        System.out.println("有四个轮子");
    }
}

class DaG implements Car{
    @Override
    public void carColor(){
        System.out.println("红色");
    }
}

public class JavaKE5 {
    public static void main(String[] args) {
        Animal myDog = new Dog();
        myDog.makeSound();
        myDog.sleep();

        Car myDaG = new DaG();
        myDaG.carColor();
        myDaG.shape();

        Vehicle tesla = new ElectricCar("Tesla Model 3");
        Charger charger = new ElectricCar("BYD Han");

        tesla.move();

        //java运算符instanceof检查tesla是否属于ElectricCar
        if (tesla instanceof ElectricCar) {
            ElectricCar car = (ElectricCar) tesla;//向下转型
            car.park();
            car.charge();
        }

        charger.charge();

        System.out.println("\n展示接口多态：");
        demonstratePolymorphism(new ElectricCar("NIO E17"));

        lesson l=new lesson();
        l.lessonBegin(new Teacher());
        l.lessonBegin(new Student());

        MainBoard mb=new MainBoard();
        mb.plugIn(new Mouse());
        mb.plugIn(new Keyboard());

        Fruit f=Factory1.getInstance("apple");
        f.eat();
    }
        //根据对象实际类型调用不同行为
        public static void demonstratePolymorphism(Object obj){
            if (obj instanceof Vehicle){
                ((Vehicle)obj).move();
            }

            if (obj instanceof Charger){
                ((Charger)obj).charge();
            }

            if (obj instanceof ElectricCar){
                ((ElectricCar)obj).park();
            }
        }
    
}

interface Vehicle{
    interface Engine{
        void start();
        void stop();
    }
    void move();
}

interface Charger{
    void charge();
}

class ElectricCar implements Vehicle,Charger{   //接口多继承
    private String model;

    class ElectricEngine implements Vehicle.Engine{
        @Override
        public void start(){
            System.out.println(model+"的电动引擎启动...");
        }
        @Override
        public void stop(){
            System.out.println(model+"的电动引擎停止...");
        }
    }

    private ElectricEngine engine;

    public ElectricCar(String model){
        this.model=model;
        this.engine=new ElectricEngine();
    }

    @Override
    public void move(){
        engine.start();
        System.out.println(model+"正在行驶...");
    }

    @Override
    public void charge(){
        System.out.println(model+"正在充电...");
    }

    public void park(){
        engine.stop();
        System.out.println(model+"已停车");
    }
}

//抽象类实例
 abstract class Person{
    public abstract void call();
}
class Teacher extends Person{
    public void call(){
        System.out.println("同学们好！");
    }
}
class Student extends Person{
    public void call(){
        System.out.println("老师好！");
    }
}
class lesson{
    public void lessonBegin(Person p){
        p.call();
    }
}

//接口类实例
interface USB{
    public void start();
    public void stop();
}
class Mouse implements USB{
    @Override
    public void start() {
        System.out.println("鼠标开始工作。");
    }

    @Override
    public void stop() {
        System.out.println("鼠标停止工作。");
    }
}
class Keyboard implements USB{
    @Override
    public void start() {
        System.out.println("键盘开始工作。");
    }
    @Override
    public void stop() {
        System.out.println("键盘停止工作。");
    }
}
class MainBoard{
    public void plugIn(USB usb){
        usb.start();
        usb.stop();
    }
}

interface Fruit{
    public void eat();
}
class Apple implements Fruit{
    @Override
    public void eat() {
        System.out.println("吃苹果。");
    }
}
class Orange implements Fruit{
    @Override
    public void eat() {
        System.out.println("吃橘子。");
    }
}
class Factory1{
    public static Fruit getInstance(String className){//静态方法直接可通过方法调用，不需要创造对象
        if ("apple".equals(className)){
            return new Apple();
        }
        if ("orange".equals(className)){
            return new Orange();
        }
        return null;
    }
}